package com.components.base.titlebar.style

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.TypedValue
import com.components.base.titlebar.TitleBarSupport

/**水波纹样式实现（对应布局属性：app:barStyle="ripple"）*/
class RippleBarStyle : TransparentBarStyle() {

    override fun getLeftTitleBackground(context: Context): Drawable {
        val drawable = createRippleDrawable(context)
        drawable?.let {
            return it
        }
        return super.getLeftTitleBackground(context)
    }

    override fun getRightTitleBackground(context: Context): Drawable {
        val drawable = createRippleDrawable(context)
        drawable?.let {
            return it
        }
        return super.getRightTitleBackground(context)
    }

    /**获取水波纹的点击效果*/
    private fun createRippleDrawable(context: Context): Drawable? {
        val typedValue = TypedValue()
        if (context.theme.resolveAttribute(
                android.R.attr.selectableItemBackgroundBorderless,
                typedValue,
                true
            )
        ) {
            return TitleBarSupport.getDrawable(context, typedValue.resourceId)
        }
        return null
    }
}